package com.sky.controller.admin;

import com.sky.anotation.AllFull;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

@Slf4j
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {

	@Autowired
	DishService dishService;

	/**
	 * 根据菜品id查询菜品
	 * @param id
	 * @return
	 */
	@ApiOperation("根据id查询菜品")
	@GetMapping("/{id}")
	public Result dishById(@PathVariable Long id) {
		DishVO dishVO = dishService.dishById(id);
		return Result.success(dishVO);
	}

	/**
	 * 菜品分页查询
	 * @param queryDTO
	 * @return
	 */
	@ApiOperation("菜品分页查询")
	@GetMapping("/page")
	public Result page(DishPageQueryDTO queryDTO) {
		PageResult pageResult = dishService.page(queryDTO);
		return Result.success(pageResult);
	}

	/**
	 * 设置菜品状态
	 * @param status
	 * @param id
	 * @return
	 */
	@ApiOperation("菜品起售或停售")
	@PostMapping("/status/{status}")
	public Result status(@PathVariable Integer status, @RequestParam Long id) {
		dishService.status(status, id);
		return Result.success(status);
	}

	/**
	 * 修改菜品
	 * @param dishDTO
	 * @return
	 */
	@ApiOperation("修改菜品")
	@PutMapping
	public Result update(@RequestBody DishDTO dishDTO) {
		dishService.update(dishDTO);
		return Result.success();
	}

	/**
	 * 新增菜品
	 * @param dishDTO
	 * @return
	 */
	@ApiOperation("新增菜品")
	@PostMapping
	public Result add(@RequestBody DishDTO dishDTO){
		dishService.add(dishDTO);
		return Result.success();
	}

	/**
	 * 批量删除菜品
	 * @param ids
	 * @return
	 */
	@ApiOperation("批量删除菜品")
	@DeleteMapping
	public Result deleteBatch(@RequestParam String ids){
		//创建容器接受处理字符串后的菜品id
		List<Long> idList = new LinkedList<>();
		log.info("ids:{}",ids);
		String[] idArray = ids.split(",");
		for(int i = 0;i < idArray.length;i++){
			log.info("idArray[{}]:{}",i, idArray[i]);
			idList.add(Long.parseLong(idArray[i]));
		}
		log.info("idList:{}",idList);
		dishService.deleteBatch(idList);
		return Result.success();
	}
}
